﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using snat.proxy;
using snat.proxy.items.exceptions;

namespace snat.proxy.items {
    public class DBConnection : Item, IComparable<DBConnection> {

        public DBEntity FromEntity { get; private set; }
        public DBEntity ToEntity { get; private set; }
        public bool IsInitial { get; private set; }
        public double Strength { get; private set; }

        public DBConnection(DataRow _row, params DataSet[] _auxilliary){
                    this.Strength = _row.Field<float>("Strength");
                    this.IsInitial = _row.Field<bool>("Initial");
                    
                    int fromID, toID;
                    fromID = _row.Field<int>("FromEntityID");
                    toID = _row.Field<int> ("ToEntityID");
                    string fromName = null;
                    string toName = null;


                    if (_auxilliary.Length > 0) {
                        DataTable peopleTable = _auxilliary[0].Tables["Table"];
 
                        try {
                            var fromData = (from person in peopleTable.AsEnumerable()
                                            where person.Field<int>("EntityID") == fromID
                                            select person).Single();
                            fromName = fromData.Field<string>("EntityName");
                        }
                        catch (Exception e) {
                       //todo     Console.WriteLine("ERRIR, {0}", e.ToString());
                        }



                        try {
                            var toData = (from person in peopleTable.AsEnumerable()
                                          where person.Field<int>("EntityID") == toID
                                          select person).Single();
                            toName = toData.Field<string>("EntityName");
                        } catch (Exception) {
                            //todo 
                        }
                    }

                    this.FromEntity = new DBEntity(fromID, fromName);
                    this.ToEntity = new DBEntity(toID, toName);
        }

        public override String ToString() {
            return String.Join(" ", "From: ", FromEntity, "To: ", ToEntity, "Strength: ", Strength, "Initial: ", IsInitial);
        }

        /// <summary>
        /// Implementation of CompareTo method
        /// </summary>
        /// <param name="other">DBConnection to compare this DBConnection with</param>
        /// <returns>0 if other has same From and To of this DBConnection, 
        /// otherwise the CompareTo of this Strength and other's Strength</returns>
        public int CompareTo(DBConnection other) {
            if (other.FromEntity.CompareTo(FromEntity) == 0 &&
                other.ToEntity.CompareTo(ToEntity) == 0) return 0;
            return other.Strength.CompareTo(Strength);
        }

        public override bool Equals(object obj) {
            return this.FromEntity.Equals((obj as DBConnection).FromEntity) && this.ToEntity.Equals((obj as DBConnection).ToEntity);
        }
    }
}
